<?php

/*
 * C订单作功能模块
 * 功能：订单
 */

namespace User\Controller;

use Think\C_Controller;

class PassportController extends C_Controller {
    
    public function __construct() {
        parent::__construct();
        $store_id = (int)$_GET['store_id'];
        if($store_id<1){
            exit;
        }
    }
    
    public function login(){
        if($_SESSION['client_id']>0){
            redirect("/user/shop?store_id=".(int)$_GET['store_id']);
        }
        $this->is_login();
        $_SESSION['s_verify'] = time();
        $this->setSession($_SESSION);
        $this->display('login');
    }
    
    /*发短信*/
    public function code(){
        $mobile = trim($_GET['mobile']);
        if(!is_mobile($mobile)){
            exit('非法手机号');
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        $re = $m->sendMsg_b($mobile);
        if($re){
            echo "true";
            exit;
        }
        exit('发送失败');
    }
    
    public function sublogin(){
        $this->is_login();
        $mobile = trim($_POST['account']);
        $code = trim($_POST['code']);
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        if(!$m->checkCode($mobile, $code)){
            redirect('/user/passport/login?msg=1&store_id='.(int)$_GET['store_id']);
        }
        $b = new \Home\Logic\UserLogic();
        $user = $b->getUserByAccount($mobile);
        if($user['id']<1){
            $user['id'] = $b->createUser($mobile);
        }
        //设置登录状态
        $_SESSION['client_id'] = $user['id'];
        $this->setSession($_SESSION);
        import('Logs', 'Log');
        $mongo = new \Log\Logs();
        $mongo->login_Log($user['id'],2);
        if(strlen($_REQUEST['referer'])){
            header("Location:".$_REQUEST['referer']);exit;
        }
        redirect(buildUrl('/user/shop'));
    }

    public function is_login(){
        if($_SESSION['client_id']>0){
            redirect(buildUrl('/user/shop'));
        }
    }
    public function foo($str) {
        $f = fopen('./public/upload/g.txt', 'a+');
        fwrite($f, $str . "\r\n");
        fclose($f);
    }





}

